#!/usr/bin/env ruby

gem_name, version_number, platform = *ARGV

abort "Usage: script/restore_version GEM_NAME VESRION_NUMBER [PLATFORM]" if gem_name.nil? || version_number.nil?

ENV["RAILS_ENV"] ||= "production"
require_relative "../config/environment"

rubygem = Rubygem.find_by_name!(gem_name)
slug = version_number
slug << "-#{platform}" if platform.present?
version = rubygem.find_version!(number: version_number, platform: platform)
raise "Version #{slug} for #{gem_name} was not found" unless version

deletion = Deletion.find_by(rubygem: gem_name, number: version_number, platform: version.platform)
raise "Deletion record for version: #{version.full_name} was not found" unless deletion

deletion.version = version
deletion.restore!
